package com.tuvi.nhamthin.helper;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import com.tuvi.nhamthin.entity.CardInfor;

import android.util.Log;


public class SoapHelper {

	/**
	 * Binhbt
	 * @param nameSpace
	 * @param method
	 * @param action
	 * @param url
	 * @param params
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public SoapObject getSoapData(String nameSpace, String method, String action, String url, Map<String, String> params) throws IOException, XmlPullParserException{
	    SoapObject request = new SoapObject(nameSpace, method); //set up request
	    @SuppressWarnings("rawtypes")
		Iterator iterator= params.entrySet().iterator();
	    while (iterator.hasNext()) {
	    	@SuppressWarnings("rawtypes")
			Map.Entry mapEntry=(Map.Entry)iterator.next();
	    	request.addProperty((String) mapEntry.getKey(), mapEntry.getValue());
		}
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
	    envelope.setOutputSoapObject(request);  //prepare request
	    @SuppressWarnings("deprecation")
		AndroidHttpTransport httpTransport = new AndroidHttpTransport(url);  
	    httpTransport.debug = true;  //this is optional, use it if you don't want to use a packet sniffer to check what the sent 
	    envelope.dotNet = true;                             //message was (httpTransport.requestDump)
	    envelope.bodyOut = request;
	    
	    httpTransport.call(action, envelope);
	    SoapObject response;
	    try{
	    	response=(SoapObject)envelope.bodyIn;
	    }catch (Exception e) {
			// TODO: handle exception
	    	response = (SoapObject) envelope.getResponse();
	    	Log.d("aaa", "aaa");
	    	return response;
		}
	    
		return response;
	}
	
	
	
}
